"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/7/18 10:32
@Description : 
 jwt的基本使用方式
"""
from datetime import datetime, timedelta

import jwt

from django_vue_back_2512 import settings

payload = {
    'username': 'admin',
    'uid': 1,
    'exp': datetime.utcnow() + timedelta(seconds=30),
}
# 创建token
# token = jwt.encode(payload=payload, key=settings.SECRET_KEY, algorithm='HS256')
# print(token)
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwidWlkIjoxLCJleHAiOjE3NTI4MDY4OTd9.PcS3Twre1myYvDOhK37BMElAEmCN9xgRAe35noGHYjI'

# 校验token  没有异常则校验通过   jwt.exceptions.InvalidSignatureError    jwt.exceptions.ExpiredSignatureError
decode_result = jwt.decode(token, key=settings.SECRET_KEY, algorithms=['HS256'])
print(decode_result.get('username'))
print(decode_result.get('uid'))
